package com.EM1.www.dao;

import com.EM1.www.po.User;
import com.EM1.www.util.Connectmysql;

import java.sql.*;
import java.util.ArrayList;
import java.util.Base64;

public class ConnectUser {
    public User selectusername(String username) throws Exception {
        Connection conn = Connectmysql.Connectmysql();
        String sql="select * from user where username=?";
        PreparedStatement pmt= conn.prepareStatement(sql);
        pmt.setString(1,username);
        User user=null;
        ResultSet res = pmt.executeQuery();
        while (res.next()){
           user=new User(res.getInt("id"),res.getString("username"),res.getString("password"),res.getBoolean("isteacher"));
            System.out.println(user);
        }
        pmt.close();
        conn.close();
        return user;
    }
    public void setUSer(String username,String password,boolean isteacher) throws Exception {
        Connection conn = Connectmysql.Connectmysql();
        String sql="insert into user(username, password, isteacher)  value (?,?,?)";
        PreparedStatement pmt = conn.prepareStatement(sql);
        pmt.setString(1,username);
        pmt.setString(2,password);
        pmt.setBoolean(3,isteacher);
        pmt.executeUpdate();
        conn.close();
        pmt.close();
    }
    public void update(String usename,int id) throws Exception {
        Connection conn=Connectmysql.Connectmysql();
        String sql="update user set username=?where id=?";
        PreparedStatement pmt = conn.prepareStatement(sql);
        pmt.setString(1,usename);
        pmt.setInt(2,id);
        pmt.executeUpdate();
    }
}
